<?php
function album_data($album_id){
  $album_id = (int)$album_id;
  
  $args = func_get_args();
  unset($args[0]);
  $fields = '`'.implode('`, `', $args).'`';
  
  $query = mysql_query("SELECT $fields FROM `albumsVU` WHERE `album_id`=$album_id AND `user_id`=".$_SESSION['user_id']);
  $query_result = mysql_fetch_assoc($query);
  foreach ($args as $field) {
    $args[$field] = $query_result[$field];
  } 
	return $args;
}

function album_check($album_id){
  $album_id = (int)$album_id;
  $query = mysql_query("SELECT COUNT(`album_id`) FROM `albumsVU` WHERE `album_id`=$album_id AND `user_id`=".$_SESSION['user_id']);
  return (mysql_result($query, 0) == 1) ? true : false;
}

function get_albums(){
  $albums = array();
  
  $albums_query = mysql_query("
  SELECT `albumsVU`.`album_id`, `albumsVU`.`timestamp`, `albumsVU`.`name`, LEFT (`albumsVU`.`description`, 50) as   
  `description`, COUNT(`imagesVU`.`image_id`) as `image_count` 
  FROM `albumsVU` 
  LEFT JOIN `imagesVU` 
  ON `albumsVU`.`album_id` = `imagesVU`.`album_id` 
  WHERE `albumsVU`.`user_id` = ".$_SESSION['user_id']."
  GROUP BY `albumsVU`.`album_id`
  ");
  

  
  while($albums_row = mysql_fetch_assoc($albums_query)) {
    $albums[] = array(
               'id' => $albums_row['album_id'],
               'timestamp' => $albums_row['timestamp'],
               'name' => $albums_row['name'],
               'description' => $albums_row['description'],
               'count' => $albums_row['image_count']
    );
    
  }
  
  return $albums;
}

function create_album($album_name, $album_description){
	$album_name = mysql_real_escape_string(htmlentities($album_name));
	$album_description = mysql_real_escape_string(htmlentities($album_description));
	
	mysql_query("INSERT INTO `albumsVU` VALUES ('', '".$_SESSION['user_id']."', UNIX_TIMESTAMP(), '$album_name', '$album_description')");
	mkdir('uploads/'.mysql_insert_id());
  chmod('uploads/'.mysql_insert_id(),0755);
  mkdir('uploads/thumbs/'.mysql_insert_id());
  chmod('uploads/thumbs/'.mysql_insert_id(),0755);
}

function edit_album($album_id, $album_name, $album_description){
  $album_id = (int)$album_id;
  $album_name = mysql_real_escape_string($album_name);
  $album_description = mysql_real_escape_string($album_description);
  
  mysql_query("UPDATE `albumsVU` SET `name`='$album_name', `description`='$album_description' WHERE `album_id`=$album_id AND `user_id`=".$_SESSION['user_id']);
}

function delete_album($album_id) {
  $album_id = (int)$album_id;
  // To do: use a method to delete all files from album folder and thumbs folder, then the direactory
  
  $dir = 'uploads/'.$album_id;
  $thumbsdir = 'uploads/thumbs/'.$album_id;
  
  if (is_dir($dir)) {
     $objects = scandir($dir);
     foreach ($objects as $object) {
       if ($object != "." && $object != "..") {
         if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
       }
     }
     reset($objects);
     rmdir($dir);
   }
   
   if (is_dir($thumbsdir)) {
     $objects = scandir($thumbsdir);
     foreach ($objects as $object) {
       if ($object != "." && $object != "..") {
         if (filetype($thumbsdir."/".$object) == "thumbsdir") rrmdir($thumbsdir."/".$object); else unlink($thumbsdir."/".$object);
       }
     }
     reset($objects);
     rmdir($thumbsdir);
   } 
  
  
  mysql_query("DELETE FROM `albumsVU` WHERE `album_id`=$album_id AND `user_id`=".$_SESSION['user_id']);
  mysql_query("DELETE FROM `imagesVU` WHERE `album_id`=$album_id AND `user_id`=".$_SESSION['user_id']);
  
  
}

?> 
